package com.hxkj.service.article.impl;

import com.alibaba.fastjson.JSONObject;
import com.hxkj.client.service.AnalyzeArticleServiceClient;
import com.hxkj.client.service.ArticleServiceClient;
import com.hxkj.client.service.ColumnServiceClient;
import com.hxkj.client.service.ManuscriptServiceClient;
import com.hxkj.domain.enums.*;
import com.hxkj.domain.po.*;
import com.hxkj.service.article.ArticleService;
import com.hxkj.token.utils.TokenUtils;
import com.hxkj.utils.JsonUtils;
import com.integral.mall.file.po.SysFile;
import com.integral.mall.file.service.SysFileService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import com.platform.common.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.hxkj.domain.Constant.CmsConstants.ARTICLEDETAIL;

/**
 * 用户service
 * <p>
 * Create by zzx on 2018/5/23 15:19
 */
@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleServiceClient articleServiceClient;

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ColumnServiceClient columnServiceClient;

    @Autowired
    private ManuscriptServiceClient manuscriptServiceClient;

    @Autowired
    private AnalyzeArticleServiceClient analyzeArticleServiceClient;

    @Autowired
    private SysFileService sysFileService;

    @Value("${cms.path}")
    private String Path;

    @Value("${cms.pathUrl}")
    private String PathUrl;

    @Value("${cms.tpl}")
    private String Tpl;

    @Value("${cms.realPath}")
    private String realPath;

    @Value("${cms.fileRoot}")
    private String fileRoot;

    //用户登录次数计数  redisKey 前缀
    private String SHIRO_LOGIN_COUNT = "shiro_login_count_";
    //用户登录是否被锁定    一小时 redisKey 前缀
    private String SHIRO_IS_LOCK = "shiro_is_lock_";

    @Override
    public ResultPageStruct<Article> getArticleList(Article article, PageTo pageParam) throws Exception {
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        ResultPageStruct<Article> result = new ResultPageStruct<>();
        ParameterDataStruct<Article> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(article);
        parameterDataStruct.setPageTo(pageParam);
        if (article.getSiteId() == null) {
            throw new CustomException(ArticleExceptionEnum.ARTICLE_SITEID_NULL);
        }
        if (article.getDelTag() == null) {
            throw new CustomException(ArticleExceptionEnum.AUTHROLE_DELAG_NULL);
        }
        if (article.getCreateTimeStart() == null && article.getCreateTimeEnd() != null) {
            throw new CustomException(ArticleExceptionEnum.ARTICLE_CREATETIMESTRAT_NULL);
        }
        if (article.getCreateTimeStart() != null && article.getCreateTimeEnd() == null) {
            throw new CustomException(ArticleExceptionEnum.ARTICLE_CREATETIMESEND_NULL);
        }
        //将前端传过来的文章创建结束时间 + 一天减一秒
        if (article.getCreateTimeEnd() != null) {
            article.setCreateTimeEnd(article.getCreateTimeEnd() + 24 * 60 * 60 * 1000 - 1);
        }

        try {
            //查询文章列表（包含文章回收站列表、文章待审核列表、文章待发布列表、文章已发布列表）
            result = articleServiceClient.getArticleList(parameterDataStruct);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取文章列表失败:{}", e.getMessage());

        }
        result.setCode(null);
        result.setMessage(null);
        return result;
    }

    @Override
    public Article getArticle(Article article) throws Exception {
        ResultDataStruct<Article> result;
        if (article.getArticleId() == null) {
            throw new CustomException(ArticleExceptionEnum.ARTICLE_ID_NULL);
        }
        try {
            // 查询一篇文章
            result = articleServiceClient.getArticle(article);
            if (result.getCode() != null) {
                throw new CustomException(ArticleExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询文章失败:{}", e);
            throw e;
        }
        return result.getData();
    }

    @Override
    public void modifyArticle(JSONObject jsonObject) throws Exception {
        ResultMessageStruct result;
        try {
            if (jsonObject == null) {
                throw new CustomMessageException(401, "没有数据需要保存");
            }
            Article article = JSONObject.toJavaObject(jsonObject, Article.class);

            //判断是否有审核状态
            if (article.getAuditType() == null) {
                throw new CustomException(ArticleExceptionEnum.AUTHROLE_AUDITTYPE_NULL);
            }
            if (article.getArticleId() == null) {
                throw new CustomException(ArticleExceptionEnum.ARTICLE_ID_NULL);
            }
            if (StringUtils.isNotEmpty(article.getTags())) {
                //Mr.zheng:验证tags的数量及其长度
                verifyTags(article.getTags());
            }
            //验证数据
            verifyData(article);
            Column column = new Column();
            column.setColumnId(article.getColumnId());
            column = columnServiceClient.getColumn(column);
            AuthUser authUser = (AuthUser) tokenUtils.getTokenUserInfo();
            //如果作者为空 取登录的用户名
            if (StringUtils.isBlank(article.getAuthor())) {
                article.setAuthor(authUser.getUserName());
            }
            article.setUpdateTime(System.currentTimeMillis());
            // 如果状态更改为立即发布,调用模板解析接口生成静态模板推送至 静态资源
            //修改文章
            result = articleServiceClient.updateArticle(article);

            if (article.getAuditType() == 0 && article.getDelTag() == 0) {
                String manuscript = parsingResources(article, column);
                if (StringUtils.isNotBlank(manuscript)) {
                    article.setManuscriptPath(manuscript);
                    articleServiceClient.updateArticle(article);


                    //修改t_manuscript表的create_status字段为1,待生成
                    Manuscript manuscript1 = getManuscript(article.getSiteId(), article.getArticleId());
                    manuscript1.setCreateStatus(1);
                    manuscriptServiceClient.saveOrUpdate(manuscript1);
                }

            }

            if (result.getCode() != null) {
                throw new CustomException(ArticleExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

    }


    //Mr.zheng:验证tags长度,tags为非必填项
    private void verifyTags(String tags) throws Exception {
        if (StringUtils.isBlank(tags)) {
            return;
        }
        String[] str = tags.split(",|，|、");
        if (str.length > 5) {
            throw new CustomException(ArticleExceptionEnum.ARTICLE_TAGS_OVER_NUM);
        }
        for (int i = 0; i < str.length; i++) {
            if (str[i].length() > 20) {
                throw new CustomMessageException(401,"亲！单个TAG标签长度不能超过20个字，请重新输入用','连接哦");
            }
        }
    }


    //文章生成静态页
    private String parsingResources(Article article, Column column) throws Exception {
        // 清除所有的自定义标签的redis缓存
        redisTemplate.delete(redisTemplate.keys("directive_*"));
        String manuscriptPath = null;
        try {
            //查询首页 静态地址
            FreeMarker model = new FreeMarker();
            model.setSiteId(article.getSiteId());
            model.setChannelId(String.valueOf(article.getColumnId()));
            model.setColumnId(article.getColumnId());
            model.setTempletId(column.getTempletId());
            model.setTime(article.getCreateTime());
            model.setAliasName("article");
            model.setArticleId(article.getArticleId());
            model.setStatus(ARTICLEDETAIL);
            model.setUserToken(((AuthUser) tokenUtils.getTokenUserInfo()).getUserId().toString());
            SysFile data = analyzeArticleServiceClient.analyzeArticle(model).getData();
            manuscriptPath = "/" + data.getFilePath();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

        return manuscriptPath;
    }

    //文章从回收站恢复
    @Override
    public void articleRecover(Article article) throws Exception {
        try {
            if (StringUtils.isBlank(article.getArticleIds())) {
                throw new CustomException(ArticleExceptionEnum.ARTICLE_ID_NULL);
            }

            //解析 articleIds 文章id String数组
            String[] articleIds = article.getArticleIds().split(",");
            for (String articleId : articleIds) {
                Article art = new Article();
                art.setArticleId(Long.valueOf(articleId));
                Article article1 = articleServiceClient.getArticle(art).getData();
                article1.setAuditType(1);
                article1.setDelTag(0);
                String json = JSONObject.toJSONString(article1);
                JSONObject jsonObject = JSONObject.parseObject(json);
                this.modifyArticle(jsonObject);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //文章审核,设置文章推荐级别
    @Override
    public void updateArticle(Article article) throws Exception {
        try {
            ResultMessageStruct result;
            if (StringUtils.isBlank(article.getArticleIds())) {
                throw new CustomException(ArticleExceptionEnum.ARTICLE_ID_NULL);
            }
            //当文章推荐或置顶时，必须上传图片
            if (article.getLocationLevel() != null && article.getLocationLevel() != 0) {
                this.checkImg(article);
            }
            if (article.getAuditType() != null && article.getAuditType() == 0) {

                //解析 articleIds 文章id String数组
                String[] articleIds = article.getArticleIds().split(",");
                for (String articleId : articleIds) {
                    Article art = new Article();
                    art.setArticleId(Long.valueOf(articleId));
                    Article article_ = articleServiceClient.getArticle(art).getData();
                    article_.setAuditType(0);
                    String json = JSONObject.toJSONString(article_);
                    JSONObject jsonObject = JSONObject.parseObject(json);
                    this.modifyArticle(jsonObject);
                }
            } else {
                //修改状态,不用生成静态
                result = articleServiceClient.articleRecover(article);
                if (result.getCode() != null) {
                    throw new CustomException(ArticleExceptionEnum.getEnumByCode(result.getCode()));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //文章置顶
    @Override
    public void articleStick(Article article) throws Exception {
        ResultMessageStruct result;
        if (StringUtils.isBlank(article.getArticleIds())) {
            throw new CustomException(ArticleExceptionEnum.ARTICLE_ID_NULL);
        }
        //置顶文章必须有图片
        if (article.getIsStick() != null && article.getIsStick() != 0) {
            this.checkImg(article);
        }
        try {
            // 文章置顶
            result = articleServiceClient.articleStick(article);
            if (result.getCode() != null) {
                throw new CustomException(SiteExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    private void checkImg(Article article) throws Exception {
        //查询推荐置顶文章是否是图片
        String[] ids = article.getArticleIds().split(",");
        for (String idStr : ids) {
            Article article1 = new Article();
            article1.setArticleId(Long.valueOf(idStr));
            article1.setSiteId(article.getSiteId());
            Article data = articleServiceClient.getArticle(article1).getData();
            if (StringUtils.isBlank(data.getImgUrl())) {
                throw new CustomMessageException(401, "标题为:\"" + data.getHeadline() + "\"的文章没有图片,请先上传图片再推荐或置顶!");
            }
        }
    }

    @Override
    public void removerArticle(Article article) throws Exception {
        ResultMessageStruct result;
        if (StringUtils.isBlank(article.getArticleIds())) {
            throw new CustomException(ArticleExceptionEnum.ARTICLE_ID_NULL);
        }
        try {

            // 移除文章至回收站
            result = articleServiceClient.removerArticle(article);
            if (result.getCode() != null) {
                throw new CustomException(SiteExceptionEnum.getEnumByCode(result.getCode()));
            }
            //删除稿件表的数据和服务器上的静态页面
            String articleIds = article.getArticleIds();
            for (String articleId : articleIds.split(",")) {
                Manuscript manuscript = getManuscript(article.getSiteId(), Long.valueOf(articleId));
                if (manuscript != null) {
                    manuscriptServiceClient.deleteManuscript(manuscript.getManuscriptId());
                    sysFileService.remove(fileRoot + manuscript.getUrlPath());
                    article.setManuscriptPath(null);
                    article.setArticleId(Long.valueOf(articleId));
                    articleServiceClient.modifyArticleAsPath(article);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //发布一篇文章
    @Override
    public void saveArticle(JSONObject jsonObject) throws Exception {
        ResultDataStruct<Article> result;
        if (jsonObject == null) {
            throw new CustomMessageException(401, "没有数据需要保存");
        }
        Article article = JSONObject.toJavaObject(jsonObject, Article.class);

        //判断是否有审核状态
        if (article.getAuditType() == null) {
            throw new CustomException(ArticleExceptionEnum.AUTHROLE_AUDITTYPE_NULL);
        }
        if (article.getColumnId() == null) {
            throw new CustomException(ColumnExceptionEnum.COLUMN_NOT_VOID);
        }
        //Mr.zheng:验证tags的数量及其长度
        if (StringUtils.isNotEmpty(article.getTags())) {
            verifyTags(article.getTags());
        }

        Column column = new Column();
        column.setColumnId(article.getColumnId());
        column = columnServiceClient.getColumn(column);
        //如果保存的文章 的状态为 待发布则 跳过字段校验
        article.setContentStatus(column.getContentStatus());
        verifyData(article);


        //如果作者为空 取登录的用户名
        if (StringUtils.isBlank(article.getAuthor())) {
            AuthUser authUser = (AuthUser) tokenUtils.getTokenUserInfo();
            article.setAuthor(authUser.getUserName());
        }
        //设置发布时间
        article.setCreateTime(System.currentTimeMillis());
        article.setUpdateTime(System.currentTimeMillis());
        //设置删除标志默认0
        article.setDelTag(0);
        try {
            //新增文章
            result = articleServiceClient.saveArticle(article);
            if (result.getCode() != null) {
                throw new CustomException(ArticleExceptionEnum.getEnumByCode(result.getCode()));
            }
            //如果是立即发布状态,调用公用模板解析接口 解析文章详情生成文章详情静态文件
            if (article.getAuditType() == 0) {
                Article article1 = new Article();
                String manuscript = parsingResources(result.getData(), column);
                if (StringUtils.isNotBlank(manuscript)) {

                    article1.setArticleId(result.getData().getArticleId());
                    article1.setManuscriptPath(manuscript);
                    articleServiceClient.modifyArticleAsPath(article1);

                    //修改t_manuscript表的create_status字段为1,待生成
                    Manuscript manuscript1 = getManuscript(article.getSiteId(), result.getData().getArticleId());
                    manuscript1.setCreateStatus(1);
                    manuscriptServiceClient.saveOrUpdate(manuscript1);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void deleteArticle(Article article) throws Exception {
        ResultMessageStruct result;
        if (StringUtils.isBlank(article.getArticleIds())) {
            throw new CustomException(ArticleExceptionEnum.ARTICLE_ID_NULL);
        }
        try {
            // 回收站彻底删除文章
            result = articleServiceClient.deleteArticle(article);
            if (result.getCode() != null) {
                throw new CustomException(SiteExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

    }


    /**
     * 新增 和 编辑 时 对文章的参数进行校验
     *
     * @param article
     * @throws CustomException
     */
    private void verifyData(Article article) throws CustomException {
        //如果审核状态 = 2（待发布）， 则跳过数据得非空验证
        if (!article.getAuditType().equals(2)) {
            if (StringUtils.isBlank(article.getHeadline())) {
                throw new CustomException(ArticleExceptionEnum.ARTICLE_HEADLINE_NULL);
            }
            if (article.getColumnId() == null) {
                throw new CustomException(ArticleExceptionEnum.AUTHROLE_COLUMNID_NULL);
            }
            if (StringUtils.isBlank(article.getSummary())) {
                throw new CustomException(ArticleExceptionEnum.AUTHROLE_ABSTRACTMSG_NULL);
            }
            if (StringUtils.isBlank(article.getDetail())) {
                throw new CustomException(ArticleExceptionEnum.AUTHROLE_PARTICULARS_NULL);
            }
            if (article.getDetail().length() > 20000) {
                throw new CustomException(ArticleExceptionEnum.ARTICLE_DETAIL_NUMBER_ERROR);
            }
            if (article.getSiteId() == null) {
                throw new CustomException(ArticleExceptionEnum.ARTICLE_SITEID_NULL);
            }
            if (article.getLocationLevel() != null && article.getLocationLevel() != 0 && StringUtils.isEmpty(article.getImgUrl())) {
                throw new CustomException(ArticleExceptionEnum.ARTICLE_IMGURL_NOT_NULL);
            }



        }
    }

    /**
     * 保存咨询或曝光
     *
     * @param article
     * @throws Exception
     */
    @Override
    public void saveRight(Article article, String msg) throws Exception {
        try {
            if (StringUtils.isBlank(msg)) {
                throw new CustomException(ArticleExceptionEnum.MSG_IS_NULL);
            }
            if (StringUtils.isBlank(article.getEmail())) {
                throw new CustomException(ArticleExceptionEnum.EMAIL_IS_NULL);
            }
            if (StringUtils.isBlank(article.getMobile())) {
                throw new CustomException(ArticleExceptionEnum.MOBILE_IS_NULL);
            }
            if (StringUtils.isBlank(article.getDetail())) {
                throw new CustomException(ArticleExceptionEnum.AUTHROLE_PARTICULARS_NULL);
            }
            if (article.getDetail().length() > 40000) {
                throw new CustomException(ArticleExceptionEnum.ARTICLE_DETAIL_NUMBER_ERROR);
            }
            if (article.getSiteId() == null) {
                throw new CustomException(ArticleExceptionEnum.ARTICLE_SITEID_NULL);
            }
            String mobile = article.getMobile();
            String remoteMsg = (String) redisTemplate.boundValueOps(mobile).get();
            //验证图片验证码和短信验证码
            if (!redisTemplate.hasKey(SHIRO_LOGIN_COUNT + mobile)) {
                redisTemplate.boundValueOps(SHIRO_LOGIN_COUNT + mobile).set(0L);
            }
            if (!redisTemplate.hasKey(SHIRO_IS_LOCK + mobile)) {
                redisTemplate.boundValueOps(SHIRO_IS_LOCK + mobile).set("UNLOCK");
            }
            Long loginCount_sms = (long) redisTemplate.boundValueOps(SHIRO_LOGIN_COUNT + mobile).get();

            if (loginCount_sms >= 5L) {
                redisTemplate.boundValueOps(SHIRO_IS_LOCK + mobile).set("LOCK");
                redisTemplate.boundValueOps(SHIRO_IS_LOCK + mobile).expire(1, TimeUnit.MINUTES);
                redisTemplate.boundValueOps(SHIRO_LOGIN_COUNT + mobile).set(0L);
            }

            if ("LOCK".equals(redisTemplate.boundValueOps(SHIRO_IS_LOCK + mobile).get())) {
                throw new CustomException(UserExceptionEnum.ACCOUNT_IS_LOCK);
            }
            //获取Redis中存储的验证码

            if (!msg.equalsIgnoreCase(remoteMsg)) {
                loginCount_sms++;
                throw new CustomException(ArticleExceptionEnum.MSG_IS_ERROR);
            }

            //设置审核状态为未审核
            article.setAuditType(1);
            //根据名称匹配类型
            Column column = new Column();
            column.setColumnName("期权咨询");
            column.setSiteId(article.getSiteId());
            Column column_consultation = columnServiceClient.getColumnByName(column);
            column.setColumnName("期权曝光");
            Column column_exposure = columnServiceClient.getColumnByName(column);
            //设置类型,曝光还是咨询 0咨询,1曝光
            if (article.getColumnId() == 0) {
                article.setColumnId(column_consultation.getColumnId());
            } else {
                article.setColumnId(column_exposure.getColumnId());
            }
            //设置发布时间
            article.setCreateTime(System.currentTimeMillis());
            article.setUpdateTime(System.currentTimeMillis());
            //设置删除标志默认0
            article.setDelTag(0);
            //设置副标题,摘要,标签
            article.setSubhead("-");
            article.setTags("维权中心");
            article.setSummary("-");
            article.setLocationLevel(0);
            article.setSourceLink("");
            article.setSource("-");
            String detail = article.getDetail();
            String imgUrl = article.getImgUrl();
            StringBuffer sb = new StringBuffer();
            if (!StringUtils.isEmpty(imgUrl)) {
                String[] imgs = imgUrl.split(",");
                for (int i = 0; i < imgs.length; i++) {
                    sb.append("<img src = " + realPath + imgs[i] + ">");
                    sb.append("</br>");
                }
            }
            article.setDetail(detail + sb.toString());
            article.setImgUrl("");

            if (StringUtils.isBlank(article.getAuthor())) {
                article.setAuthor("-");
            }
            //保存文章
            String json = JSONObject.toJSONString(article);
            JSONObject jsonObject = JSONObject.parseObject(json);
            this.saveArticle(jsonObject);

            //登录成功后清空该账户的验证码错误次数
            redisTemplate.boundValueOps(SHIRO_LOGIN_COUNT + mobile).set(0L);
            redisTemplate.boundValueOps(SHIRO_IS_LOCK + mobile).set("UNLOCK");
            redisTemplate.delete(mobile);

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public Integer getRightsCount(Article article) throws Exception {
        if (article.getSiteId() == null) {
            throw new CustomException(ArticleExceptionEnum.ARTICLE_SITEID_NULL);
        }
        if (article.getColumnId() == null) {
            throw new CustomException(ArticleExceptionEnum.COLUMNID_IS_NULL);
        }
        ResultDataStruct<Integer> result;

        try {
            result = articleServiceClient.getRightsCount(article);
            if (result.getCode() != null) {
                throw new CustomException(ArticleExceptionEnum.getEnumByCode(result.getCode()));
            }
            return result.getData();
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void reloadArticle(Article article) throws Exception {
        Article article1 = new Article();
        article1.setSiteId(article.getSiteId());
        //获取所有文章
        List<Article> articleList = articleServiceClient.getArticleInMonth(article1);
        for (Article article_ : articleList) {
            Column column = new Column();
            column.setColumnId(article_.getColumnId());
            column = columnServiceClient.getColumn(column);
            if (column == null) continue;
            article_.setDateFormat(DateUtils.toDateToString(new Date(), "yyyy-MM-dd"));
            FreeMarker model = new FreeMarker();
            Map<String, Object> map = JsonUtils.transBean2Map(article_);
            model.setSiteId(article_.getSiteId());
            model.setChannelId(String.valueOf(article_.getColumnId()));
            model.setTempletId(column.getTempletId());
            model.setAliasName("article");
            model.setArticleId(article_.getArticleId());
            model.setColumnId(article_.getColumnId());
            model.setStatus(ARTICLEDETAIL);
            model.setMaps(map);
            model.setUserToken(((AuthUser) tokenUtils.getTokenUserInfo()).getUserId().toString());
            SysFile data = analyzeArticleServiceClient.analyzeArticle(model).getData();


            Article art = new Article();
            art.setArticleId(article_.getArticleId());
            art.setSiteId(article_.getSiteId());
            //Mr.zheng:如果有外链，则将文章详情的页面路径设置为外链路径
            if (StringUtils.isNotEmpty(article_.getExternalLink())) {
                art.setManuscriptPath(article_.getExternalLink());
            } else {
                art.setManuscriptPath("/" + data.getFilePath());
            }
            articleServiceClient.modifyArticleAsPath(art);

        }
    }

    //实时更新点击率  zhengyi
    @Override
    public Article modifyHeat(Article article) throws Exception {
        if (article.getSiteId() == null) {
            throw new CustomException(ArticleExceptionEnum.ARTICLE_SITEID_NULL);
        }
        if (article.getArticleId() == null) {
            throw new CustomException(ArticleExceptionEnum.ARTICLE_ID_NULL);
        }
        try {
            ResultDataStruct<Article> resultDataStruct;
            resultDataStruct = articleServiceClient.modifyHeat(article);
            if (resultDataStruct.getCode() != 200) {
                throw new CustomException(ArticleExceptionEnum.getEnumByCode(resultDataStruct.getCode()));
            }

            article = resultDataStruct.getData();
            //重新生成静态页
            Column column1 = new Column();
            column1.setColumnId(article.getColumnId());
            Column column = columnServiceClient.getColumn(column1);
            String manuscript = parsingResources(article, column);
            article.setManuscriptPath(manuscript);
            articleServiceClient.modifyArticleAsPath(article);
            return article;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //根据站点id和文章id查询对应的静态地址信息Manuscript
    private Manuscript getManuscript(Long siteId, Long articleId) throws Exception {
        Manuscript manuscript = new Manuscript();
        manuscript.setAlisName(ARTICLEDETAIL + "" + siteId + articleId);
        manuscript.setStatus(ARTICLEDETAIL);
        manuscript.setSiteId(siteId);
        Manuscript manuscript1 = manuscriptServiceClient.getManuscript(manuscript).getData();
        return manuscript1;
    }
}
